package com.eascs.pay.controller;

import java.text.DecimalFormat;
import java.text.Format;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.pay.entity.O2oPayEntity;
import com.eascs.pay.form.O2oPayForm;
import com.eascs.pay.service.impl.O2oPayServiceImpl;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.MessageRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/pay")
public class O2oPayController extends BaseMasterViewController{

	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "payIndex");
	}
	
	@Autowired
	O2oPayServiceImpl payService;

	@SuppressWarnings("unused")
	@RequestMapping(value = "/index")
	public String index(O2oPayForm o2oPayForm) {
		o2oPayForm.setPageSize(10);
		String url = "/pay/index";
		if (null != o2oPayForm) {
			String billCode = o2oPayForm.getBillCode() == null ? "" : o2oPayForm.getBillCode();
			String borrowerName = o2oPayForm.getBorrowerName() == null ? "" : o2oPayForm.getBorrowerName();
			String payType = o2oPayForm.getPayType() == null ? "" : o2oPayForm.getPayType();
			String loanapplybillcode = o2oPayForm.getLoanapplybillcode() == null ? "" : o2oPayForm.getLoanapplybillcode();
			String status = o2oPayForm.getStatus() == null ? "" : o2oPayForm.getStatus();
			String createstartdate = o2oPayForm.getCreatestartdate() == null ? "" : o2oPayForm.getCreatestartdate();
			String createenddate = o2oPayForm.getCreateenddate() == null ? "" : o2oPayForm.getCreateenddate();
			url = UrlUtil.appendUrl(url, "billCode", billCode);
			url = UrlUtil.appendUrl(url, "borrowerName", borrowerName);
			url = UrlUtil.appendUrl(url, "payType", payType);
			url = UrlUtil.appendUrl(url, "status", status);
			url = UrlUtil.appendUrl(url, "loanapplybillcode", loanapplybillcode);
			url = UrlUtil.appendUrl(url, "createstartdate", createstartdate);
			url = UrlUtil.appendUrl(url, "createenddate", createenddate);
			url = url + "&pageIndex=";
		} else {
			url = url + "?pageIndex=";
		}
		PageResult<Object[]> pageResult = payService.findByPage(o2oPayForm);
		SelectDataOption[] selectDataOptions = { null, null, null, null, null, null, null, new SelectDataOption("OTO_PAY_TYPE"), new SelectDataOption("PAY_STATUS"),null };
		String[] titles = { "单据号", "终端贷贷款单号","借款人姓名", "经营单位", "放款金额(万元)", "借款期限(月)", "收款人", "付款类型", "状态","创建时间" };
		AjaxButton button = new AjaxButton(null, "/rpay/detail?id=");
		Button start = ButtonHelper.createAjaxButton("发起付款流程", "/rpay/startPay?id=", "startPayButton");
		start.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[9].equals(StatusContexts.O2O_NEW)) {
					return true;
				} else
					return false;
			}
		});
		Button[] titleButtons = new Button[] { button };
		//Button[] moreButtons = new Button[] { start };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, url);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setTitleButtons(titleButtons);
		//paginationInfo.setMoreButtons(moreButtons);
		paginationInfo.setFormats(new Format[] { null, null, null, null, new DecimalFormat(",##0.00"), null, null, null, null });
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", o2oPayForm);
		return "/pay/index";
	}
	
	@RequestMapping(value = "/find/{id}")
	public BaseRestResult<O2oPayEntity> selectPay(@PathVariable("id") String id) {
		return MessageRestResult.buildRestResult(payService.findById(id));
	}

	/**
	 * 新增
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/save")
	public BaseRestResult<String> addPay(O2oPayEntity entity) {
		int i = payService.saveEntityById(entity);
		if (i > 0) {
			return MessageRestResult.info("add success!");
		}
		return MessageRestResult.warn("add fail!");
	}

	/**
	 * 更新
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update")
	public BaseRestResult<String> updatePay(O2oPayEntity entity) {
		int i = payService.saveEntityById(entity);
		if (i > 0) {
			return MessageRestResult.info("add success!");
		}
		return MessageRestResult.warn("add fail!");
	}

	/**
	 * 更新
	 * 
	 * @param entity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public BaseRestResult<String> deletePay(O2oPayEntity entity) {
		int i = payService.deleteEntityById(entity);
		if (i > 0) {
			return MessageRestResult.info("add success!");
		}
		return MessageRestResult.warn("add fail!");
	}

}
